package com.riant.controller;

import com.riant.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Classname CustomerController
 * @Description TODO
 * @Date 2022/5/1 12:00
 * @Created by 张斌
 */
@Controller
public class CustomerController {
    private final Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping(path = {"/showCustomer"}, method = RequestMethod.GET)
    public String showCustomer() {
        logger.info("forward转发到customer.jsp");
        return "forward:/static/view/customer.jsp?msg=hello";
    }

    /**
     * @ModelAttribute:针对form表单提交Post请求
     * */
    @RequestMapping(path = {"/save1"}, method = RequestMethod.POST)
    public String save1(User user, Model model) {
        logger.info("USER:{}",user.toString());
        model.addAttribute("msg","接收成功");
        return "success";
    }
    @RequestMapping(path = {"/save2"}, method = RequestMethod.POST)
    public String save2(@RequestParam Map<String,Object> user) {
        logger.info("id:{}",user.get("id"));
        logger.info("name:{}",user.get("name"));
        logger.info("age:{}",user.get("age"));
        return "success";
    }
    @RequestMapping(path = {"/save3"}, method = RequestMethod.POST)
    public String save3( String id,String name,String age) {
        logger.info("id:{}",id);
        logger.info("name:{}",name);
        logger.info("age:{}",age);
        return "success";
    }

    @RequestMapping(path = {"/save4"}, method = RequestMethod.POST)
    public String save4(HttpSession httpSession, InputStream inputStream,Map map) throws IOException {
        logger.info("session:{}",httpSession.getId());
        map.put("msg","save4 success");
        return "success";
    }

    @RequestMapping(path = {"/save5"}, method = RequestMethod.POST)
    public String save5(ModelMap map) throws IOException {
        //下面两种添加数据都可以渲染视图。
        map.addAttribute("msg","save5 success");
        //map.put("msg","save4 success");
        return "success";
    }
}
